﻿Imports System.Data
Imports System.Data.SqlClient

Partial Class order
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents dgcus As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lbgiaohang As System.Web.UI.WebControls.Label
    Protected WithEvents ttlienhe As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtmonth As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtday As System.Web.UI.WebControls.TextBox
    Protected WithEvents btn2 As System.Web.UI.WebControls.Button
    Protected WithEvents cboNam As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtEnd As System.Web.UI.WebControls.TextBox
    Protected WithEvents cboemail As System.Web.UI.WebControls.DropDownList


    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object



    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim myCusDS As New DataSet
    Dim ssqlcthd As String = "select * from vwctdonhang order by iddonhang desc"
    Dim sT2 As Date
    Dim iWeek As Integer = 0

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            Session("Back") = Request.UrlReferrer.ToString
            loadInfo()
            BindGrid()
        End If
    End Sub

    Private Sub loadInfo()
        If IsNumeric(Request.QueryString("w")) Then
            iWeek = Request.QueryString("w")
        End If
        Select Case Now.DayOfWeek
            Case DayOfWeek.Monday
                sT2 = Now.AddDays(-3)
            Case DayOfWeek.Tuesday
                sT2 = Now.AddDays(-4)
            Case DayOfWeek.Wednesday
                sT2 = Now.AddDays(-5)
            Case DayOfWeek.Thursday
                sT2 = Now.AddDays(-6)
            Case DayOfWeek.Friday
                sT2 = Today()
            Case DayOfWeek.Saturday
                sT2 = Now.AddDays(-1)
            Case DayOfWeek.Sunday
                sT2 = Now.AddDays(-2)
        End Select

        sT2 = sT2.AddDays(7 * iWeek)
        sT2 = sT2.Date()
        Dim sLink, sPre, sNext As String
        sLink = Request.RawUrl()

        If Request.QueryString("w") = "" Then
            sLink += IIf(InStr(sLink, "?") > 0, "&w=0", "?w=0")
        End If

        sPre = Replace(sLink, "w=" + iWeek.ToString, "w=" + (iWeek - 1).ToString)
        sNext = Replace(sLink, "w=" + iWeek.ToString, "w=" + (iWeek + 1).ToString)
        lblWeek.Text = ""
        lblWeek.Text = "<a href=" + sNext + " class=""linkpage""> Next </a>"
        lblWeek.Text += "<span class=""linkpage"">" + "Tuần :" + Sys2VNDate(sT2.ToShortDateString()) + " - " + Sys2VNDate(sT2.AddDays(6).ToShortDateString()) + "</span>"
        lblWeek.Text += "<a href=" + sPre + " class=""linkpage""> Pre </a>"

    End Sub

    Private Sub BindGrid()
        Dim records As Int16 = 10, totalRecords As Int16 = 0
        Dim Tongdoanhthu As Double
        Dim strWhere As String = " where 1=1 and dh.xoa<>1 and ngay >='" & sT2.Date.ToString & "' and ngay <'" & sT2.AddDays(7).Date.ToString & "'"
        Dim strwhere1 As String = ""
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select convert(varchar(10),ngay,103) as ngay,convert(varchar(10),ngaygiao,103) as ngaygiao,isnull(ttlienhe, N'Chưa liên hệ') as ttlienhe, isnull(dh.doanhthu,0) doanhthu,  dh.iddonhang,dh.yeucau,isnull( dh.tongtien,0) as tongtien, isnull(dh.giam,0) as giam ,kh.*, (isnull(tongtien,0)- isnull(giam,0)) as conlai  from donhang dh left join khachhang kh  On kh.idkhachhang=dh.idkhachhang left join lienhe lh On lh.idlienhe = dh.idlienhe" & strWhere & " order by iddonhang desc"
        Dim ssql1 As String = "select count(*) from donhang as dh " & strWhere

        myConn.Open()
        Dim myCommand As New SqlCommand(ssql1, myConn)
        totalRecords = myCommand.ExecuteScalar
        myCommand.CommandText = "select isnull( sum(tongtien),0) from donhang as dh " & strWhere
        Tongdoanhthu = myCommand.ExecuteScalar()
        lblDoanhthu.Text = "<span class=""linkpage"">" & "Tổng giá trị đơn hàng: <b>" & FormatNumber(Tongdoanhthu, 0) & "</b></span>"
        myCommand.CommandText = "select isnull( sum(doanhthu),0) from donhang as dh " & strWhere
        Tongdoanhthu = myCommand.ExecuteScalar()
        lblDoanhthu.Text += "<span class=""linkpage"">" & "Doanh thu thực: <b>" & FormatNumber(Tongdoanhthu, 0) & "</b></span>"
        myCommand.Dispose()
        Dim myOrderCom As New SqlCommand(ssqlcthd, myConn)
        Dim myOrderAd As New SqlDataAdapter(ssqlcthd, myConn)
        myOrderAd.Fill(myCusDS, ssqlcthd)
        Dim myAdapter As New SqlDataAdapter(sSql, myConn)
        Dim myDataset As New DataSet
        Dim index As Int16 = 1
        Label2.Text = ""
        If IsNumeric(Request.QueryString("p")) Then
            index = Request.QueryString("p")
        End If

        'totalRecords = myDataset.Tables(sSql).Rows.Count
        Label2.Text = "<div align=right><b>"
        If totalRecords >= 1.5 * records Then
            'phan trang
            Dim i As Int16

            For i = 1 To totalRecords \ records + IIf(totalRecords Mod records = 0, 0, 1)
                If i = index Then
                    Label2.Text += "&nbsp;<font color=ff0000>" + i.ToString + "</font>"
                Else
                    Label2.Text += "&nbsp;" + "<a href=""" & "order.aspx?w=" & iWeek.ToString & "&p=" & i.ToString + """> " + i.ToString + " </a>"
                End If
            Next
            'Label2.Text += "</b> Total : " & totalRecords.ToString & " records </div>"
        Else
            records = totalRecords
        End If
        If records <> 0 Then
            If index < 1 Or index > totalRecords \ records + IIf(totalRecords Mod records = 0, 0, 1) Then
                index = 1
            End If
        End If
        Label2.Text += "Total : " & totalRecords.ToString & " records</b></div>"
        Label3.Text = Label2.Text

        myAdapter.Fill(myDataset, (index - 1) * records, records, sSql)
        DataGrid1.DataSource = myDataset
        DataGrid1.DataBind()
        myConn.Close()
    End Sub

    Public Sub DataGrid1_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub

    Function getDetailOrderSource(ByVal idorder As Int64) As DataView
        'Find out the CategoryID
        Dim myCmDV As DataView
        myCmDV = myCusDS.Tables(ssqlcthd).DefaultView
        myCmDV.RowFilter = " iddonhang=" & idorder
        Return myCmDV
    End Function

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        Dim strUrl As String
        If Page.IsValid Then
            strUrl = Request.RawUrl
            If strUrl.IndexOf("?") > 0 Then
                strUrl = Left(strUrl, strUrl.IndexOf("?"))
            End If
            Response.Redirect(strUrl)
        End If
    End Sub

    Protected Sub btnToExcelByResponse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '   Variables declaration
        Dim dsExport As New DataSet
        Dim tw As New System.IO.StringWriter
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        Dim dgGrid As New DataGrid

        'dgGrid.DataSource = getData()
        '   Report Header
        hw.WriteLine("<b><u><font size='5'> Student Marking Report </font></u></b>")

        '   Get the HTML for the control.
        dgGrid.HeaderStyle.Font.Bold = True
        dgGrid.DataBind()
        dgGrid.RenderControl(hw)

        '   Write the HTML back to the browser.
        Response.ContentType = "application/vnd.ms-excel"
        Me.EnableViewState = False
        Response.Write(tw.ToString())
        Response.End()
    End Sub

End Class
